package com.ukao.cashregister.rfid;

import android.hardware.usb.UsbDevice;
import android.util.Log;

import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.usbrfidreader.UsbrfidreaderHelper;
import com.ukao.cashregister.utils.CheckUsbDeviceUtils;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

import static com.ukao.cashregister.eventbus.MembershipCardEvent.Message.fali;

/**
 * Created by herego on 2017/6/21.
 */

public class MembershipCardHelper {


    private int mUserIdStoreBlockIndex = 0x04; //16个字节
    private   RfidHelper mRfidHelper;
    public String mCardId = "";
    public String mUserId = "";
    public static MembershipCardHelper mMembershipCardHelper;
    private  UsbrfidreaderHelper mUsbrfidreaderHelper;




    public static MembershipCardHelper getInstance(){
        if(mMembershipCardHelper == null) {
            mMembershipCardHelper= new MembershipCardHelper();
        }
        return  mMembershipCardHelper;
    }






    public void readUserId(){
        UsbDevice mUsbDevice= CheckUsbDeviceUtils.getConnectCardReader();
        if (mUsbDevice!=null){
            mUsbrfidreaderHelper=UsbrfidreaderHelper.getInstance();
            mUsbrfidreaderHelper.readRfid();
        }else{
            mRfidHelper=RfidHelper.getInstance();
            RfidHelper.getInstance().openSerialPort();
            mRfidHelper.readRfid(this.mUserIdStoreBlockIndex);
        }
    }





    public void bindCard() {
        mRfidHelper.scanCard();
    }

    public void writeCard(String userId) {
        mRfidHelper.writeRfid(userId,mUserIdStoreBlockIndex);
    }






    public  void  onDestroy(){
        mMembershipCardHelper=null;
        if (mRfidHelper!=null){
            mRfidHelper.closeSerialPort();
            mRfidHelper=null;

        }
        if (mUsbrfidreaderHelper!=null){
            mUsbrfidreaderHelper=null;
        }

    }



}
